package com.mengtian_tech.tju.dataquery.api.service;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.github.pagehelper.util.StringUtil;

/**
 * 处理行人门禁数据
 */
@Service
public class GateDataHandleService {
	
	@Autowired
	@Qualifier("jdbcTemplateDataAi")
	JdbcTemplate jdbcTemplateDataAi;
	
 
	
	/**
	 * 同步数据
	 */
	public synchronized int handle() {
		 
		
		int count = 0;
		
		List<Map<String,Object>> list =  this.jdbcTemplateDataAi.
				queryForList("SELECT * from data_ai.user_gate_log where date_on is null limit 0,2000");
		count = list.size();
		
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		try {		
			for(Map<String,Object> item:list) {
				
				BigInteger id = (BigInteger) item.get("id");
				String srcName = (String) item.get("src_name");
			
				//校区
				int schoolIndex = srcName.indexOf("校区");
				String school = null;
				if(schoolIndex != -1) {
					school = srcName.substring(0,schoolIndex);
				}else {
					school = "error";
				}
				
				//大门
				int gateIndex = srcName.indexOf("门");
				String gate = null;
				if(gateIndex != -1) {
					gate = srcName.substring(schoolIndex+1+1,gateIndex+1);
				}else {
					gate =  "error";
				}
				
				//方向
				String direction = srcName.indexOf("入口") != -1?"入":"出";
				
				//日期
				Date createTime = (Date) item.get("created_at");
				String dateOn = sdf.format(createTime);

				//小时
				Calendar c = Calendar.getInstance();
				c.setTime(createTime);
				int hourOn = c.get(Calendar.HOUR_OF_DAY);

				
				this.jdbcTemplateDataAi.update("update  data_ai.user_gate_log set school_area=?,gate_name=?,date_on=?,hour_on=?,direction=?  where id = ?",
						school,gate,dateOn,hourOn,direction, id);
				
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		} 
		
		System.out.println("deal gate data end , count="+count);
		return count;
		
	}
	
	/**
	 * 处理报表
	 */
	public synchronized BigInteger handleRpt(BigInteger startId) {
		 if(startId == null) startId = BigInteger.ZERO;
		 
		 System.out.println(new Date() + "准备处理行人报表:");
		 
		BigInteger lastId = null;
		List<Map<String,Object>> list =  this.jdbcTemplateDataAi.
				queryForList("SELECT id,date_on,school_area,gate_name from user_gate_log where rpt_flag is null and id > ? and direction = '入' order by id asc  limit 0,500",startId);
		
		System.out.println("deal gate data rpt, id="+startId);
		
		
		try {		
			for(Map<String,Object> item:list) {
				
				lastId = (BigInteger) item.get("id");
				String date_on = (String) item.get("date_on");
				String school_area = (String) item.get("school_area");
				String gate_name = (String) item.get("gate_name");
			
				List<Map<String,Object>> rptItemList = null;
				if(StringUtil.isEmpty(school_area)) {
					rptItemList = this.jdbcTemplateDataAi.queryForList(
							"select id,user_count  from rpt_user_gate  where date_on = ? and school_area is null and gate_name=?", 
							date_on,gate_name);
				}else {
					rptItemList = this.jdbcTemplateDataAi.queryForList(
							"select id,user_count  from rpt_user_gate  where date_on = ? and school_area = ? and gate_name=?", 
							date_on,school_area,gate_name);
				}
				
				
				if(rptItemList.size() == 0) {
					this.jdbcTemplateDataAi.update("insert into rpt_user_gate (date_on,school_area,gate_name,user_count) "
							+ " values(?,?,?,?)",date_on,school_area,gate_name,1);
				}else {
					Map<String,Object> rpt = rptItemList.get(0);
					this.jdbcTemplateDataAi.update("update rpt_user_gate  set user_count = ? where id = ? ",
						Integer.parseInt(rpt.get("user_count").toString())+1,rpt.get("id"));
				}
				
				
				//更新已处理
				this.jdbcTemplateDataAi.update("update  data_ai.user_gate_log set rpt_flag= 1  where id = ?",
						lastId);
				
			}
			
		}catch (Exception e) {
			System.out.println(new Date()+"handle rpt error<<<<<< ");
			System.out.println(jdbcTemplateDataAi.queryForList("SELECT count(1) from user_gate_log where rpt_flag is null and id > 125 and direction = '入' order by id asc  limit 0,2000"));
			e.printStackTrace();
			return null;
		} 
		
		return lastId;
		
	}

	 
	
}
